using System;
using System.Collections.Generic;
using Gameplay.PVE.Effect;
using LogSystem;
using UnityEngine;

namespace Yoozoo.Core.Common
{
    [Serializable]
    public class ParticleGradingData
    {
        public List<ParticleSystem> disableParticles;
        public List<MeshRenderer> disableMeshRenderers;
        public List<Animator> disableAnimators;
        public List<Animation> disableAnimations;
        
        [HideInInspector]
        public List<ParticleSystemData> psData;

        [HideInInspector] public List<int> disableParticlesIndex;
        [HideInInspector] public List<int> disableMeshRenderersIndex;
        [HideInInspector] public List<int> disableAnimatorsIndex;
        [HideInInspector] public List<int> disableAnimationsIndex;
        
        public void InitGrade(PveEffectController controller)
        {
            disableParticlesIndex = new List<int>();
            for (int i = 0; i < disableParticles.Count; i++)
            {
                var ps = disableParticles[i];
                for (int j = 0; j < controller.particleSystems.Length; j++)
                {
                    if (ps == controller.particleSystems[j] && !disableParticlesIndex.Contains(j))
                    {
                        disableParticlesIndex.Add(j);
                        break;
                    }
                }
            }
            disableParticlesIndex.Sort();
            
            disableMeshRenderersIndex = new List<int>();
            for (int i = 0; i < disableMeshRenderers.Count; i++)
            {
                var mr = disableMeshRenderers[i];
                for (int j = 0; j < controller.meshRenderers.Length; j++)
                {
                    if (mr == controller.meshRenderers[j] && !disableMeshRenderersIndex.Contains(j))
                    {
                        disableMeshRenderersIndex.Add(j);
                        break;
                    }
                }
            }
            disableMeshRenderersIndex.Sort();
            
            disableAnimationsIndex = new List<int>();
            for (int i = 0; i < disableAnimations.Count; i++)
            {
                var a = disableAnimations[i];
                for (int j = 0; j < controller.animations.Length; j++)
                {
                    if (a == controller.animations[j] && !disableAnimationsIndex.Contains(j))
                    {
                        disableAnimationsIndex.Add(j);
                        break;
                    }
                }
            }
            disableAnimationsIndex.Sort();
            
            disableAnimatorsIndex = new List<int>();
            for (int i = 0; i < disableAnimators.Count; i++)
            {
                var a = disableAnimators[i];
                for (int j = 0; j < controller.animators.Length; j++)
                {
                    if (a == controller.animators[j] && !disableAnimatorsIndex.Contains(j))
                    {
                        disableAnimatorsIndex.Add(j);
                        break;
                    }
                }
            }
            disableAnimatorsIndex.Sort();
        }

    }
    
    [Serializable]
    public class ParticleSystemData
    {
        public int maxCount;
        public float duration;
        public bool isLoop;
    }
    
    public class ParticleGrading : MonoBehaviour
    {
        [Header("品质为Perfect时启用")]
        public ParticleGradingData level0;
        [Header("品质为High、Middle时启用")]
        public ParticleGradingData level1;
        [Header("品质为Low、VeryLow时启用")]
        public ParticleGradingData level2;

        private ParticleGradingData _currentLevel;

        public void InitData(PveEffectController controller)
        {
            level0.InitGrade(controller);
            level1.InitGrade(controller);
            level2.InitGrade(controller);
        }

    
        public void ChangeLevel(int lv)
        {
            ParticleGradingData pd = level2;
            if (lv == 0) pd = level0;
            else if (lv == 1) pd = level1;

            if (_currentLevel != null)
            {
                // 还原设置
                if (_currentLevel.disableParticles!=null)
                {
                    for (int i = 0; i < _currentLevel.disableParticles.Count; i++)
                    {
                       var ps = _currentLevel.disableParticles[i];
                       var psData = _currentLevel.psData[i];

                       if (ps!=null && psData!=null)
                       {
                           var psMain = ps.main;
                           psMain.maxParticles = psData.maxCount;
                           //psMain.duration = psData.duration;
                           psMain.loop = psData.isLoop;
                       }
                    }
                }

                if (_currentLevel.disableMeshRenderers!=null)
                {
                    foreach (var disableMeshRenderer in _currentLevel.disableMeshRenderers)
                    {
                        if (disableMeshRenderer)
                        {
                            disableMeshRenderer.enabled = true;
                        }
                    }
                }

                if (_currentLevel.disableAnimators!=null)
                {
                    foreach (var disableAnimator in _currentLevel.disableAnimators)
                    {
                        if (disableAnimator)
                        {
                            disableAnimator.enabled = true;
                        }
                    }
                }

                if (_currentLevel.disableAnimations!=null)
                {
                    foreach (var disableAnimation in _currentLevel.disableAnimations)
                    {
                        if (disableAnimation)
                        {
                            disableAnimation.enabled = true;
                        }
                    }
                }
                
            }
            
            _currentLevel = pd;
            
            if (_currentLevel.disableParticles != null)
            {
                foreach (var particle in _currentLevel.disableParticles)
                {
                    if (particle)
                    {
                        var particleMain = particle.main;
                        particleMain.maxParticles = 0;
//                        particleMain.duration = 0;
                        particleMain.loop = false;
                        
                        // 暂停可能会对playOnAwake的粒子系统有影响 
                        //particle.Pause();
                    }
                }
            }


            if (_currentLevel.disableMeshRenderers!=null)
            {
                foreach (var disableMeshRenderer in _currentLevel.disableMeshRenderers)
                {
                    if (disableMeshRenderer)
                    {
                        disableMeshRenderer.enabled = false;
                    }
                }
            }

            if (_currentLevel.disableAnimators!=null)
            {
                foreach (var disableAnimator in _currentLevel.disableAnimators)
                {
                    if (disableAnimator)
                    {
                        disableAnimator.enabled = false;
                    }
                }
            }

            if (_currentLevel.disableAnimations!=null)
            {
                foreach (var disableAnimation in _currentLevel.disableAnimations)
                {
                    if (disableAnimation)
                    {
                        disableAnimation.enabled = false;
                    }
                }
            }
            
        }


        private void Awake()
        {
            if (level0.disableParticles!=null)
            {
                for (int i = 0; i < level0.disableParticles.Count; i++)
                {
                    var ps = level0.disableParticles[i];
                    var psd = level0.psData[i];

                    if (level0.psData.Count != level0.disableParticles.Count)
                    {
                        // 理论上不会走到这一步，加个保护。
                        Debug.LogError(LogModule.Quality,$"{transform.name}: 粒子系统信息数量不匹配...");
                        level0.psData = new List<ParticleSystemData>();
                        for (int j = 0; j < level0.disableParticles.Count; j++)
                        {
                            level0.psData.Add(new ParticleSystemData());
                        }
                    }
                    
                    if (ps!=null && psd != null)
                    {
                        var main = ps.main;
                        psd.duration = main.duration;
                        psd.maxCount = main.maxParticles;
                        psd.isLoop = main.loop;
                    }
                }
            }   

            if (level1.disableParticles!=null)
            {
                for (int i = 0; i < level1.disableParticles.Count; i++)
                {
                    var ps = level1.disableParticles[i];
                    var psd = level1.psData[i];
                    if (ps!=null && psd != null)
                    {
                        var main = ps.main;
                        psd.duration = main.duration;
                        psd.maxCount = main.maxParticles;
                        psd.isLoop = main.loop;
                    }
                }
            }   
            
            if (level2.disableParticles!=null)
            {
                for (int i = 0; i < level2.disableParticles.Count; i++)
                {
                    var ps = level2.disableParticles[i];
                    var psd = level2.psData[i];
                    if (ps!=null && psd != null)
                    {
                        var main = ps.main;
                        psd.duration = main.duration;
                        psd.maxCount = main.maxParticles;
                        psd.isLoop = main.loop;
                    }
                }
            }
            if (QualityHelper.ParticleQualityListener!=null)
            {
                QualityHelper.ParticleQualityListener.AddParticleGrading(this);
            }
        }

        private void OnDestroy()
        {
            if (QualityHelper.ParticleQualityListener != null)
            {
                QualityHelper.ParticleQualityListener.RemoveParticleGrading(this);
            }
        }


#if UNITY_EDITOR
        private void OnValidate()
        {
            // 在非运行时将数据对象创建并序列化
            level0.psData = new List<ParticleSystemData>();
            if (level0.disableParticles!=null)
            {
                for (int i = 0; i < level0.disableParticles.Count; i++)
                {
                    level0.psData.Add(new ParticleSystemData());
                }
            }   

            level1.psData = new List<ParticleSystemData>();
            if (level1.disableParticles!=null)
            {
                for (int i = 0; i < level1.disableParticles.Count; i++)
                {
                    level1.psData.Add(new ParticleSystemData());
                }
            }   
            
            level2.psData = new List<ParticleSystemData>();
            if (level2.disableParticles!=null)
            {
                for (int i = 0; i < level2.disableParticles.Count; i++)
                {
                    level2.psData.Add(new ParticleSystemData());
                }
            }   
        }
#endif
        
    }
}